iT邦幫忙

2021 iThome 鐵人賽

DAY 9
1
AI & Data

建立FF14資訊Discord chatbot系列 第 9

[DAY 09]Discord Bot回覆帶入圖片方法

  • 分享至 

  • xImage
  •  

今天分享如何把圖片放進先前的翻譯及拍賣查詢功能

在universalis上每個物品都會有張圖片

點擊F12檢查圖片連結後發現他跟物品網址一樣是用ID來辨識

接下來新增下列程式碼

embed.set_thumbnail(url=<圖片連結>)

附上完整程式碼

import discord
import requests
import pandas as pd
import pickle
import difflib
from discord.ext import commands
import ahocorasick
from bs4 import BeautifulSoup
from dotenv import load_dotenv
import os
import chardet


if __name__ == '__main__':

    
    #讀取Token
    load_dotenv()
    TOKEN = os.getenv('DISCORD_TOKEN')
    
    #讀取字典
    with open("item_dict.pkl", "rb") as tf:
        item_dict = pickle.load(tf)

    wordlist = [word for word in item_dict]
    

    
    
    #client = discord.Client()
    intents = discord.Intents.default()
    intents.members = True
    client = discord.Client(intents=intents)
    # client = commands.Bot(command_prefix='.',intents =intents)
    embed = discord.Embed()
    # bot = discord.ext.commands.Bot(command_prefix = "your_prefix")

    @client.event
    #當有訊息時
        async def on_message(message):
            #排除自己的訊息,避免陷入無限循環
            if message.author == client.user:
                return
                

            #翻譯
            if message.content.startswith('?tr '):
                user_word = message.content.replace('?tr ',"")
                user_word = user_word.lstrip().rstrip()

                if user_word in item_dict:

                    if "簡體中文" in item_dict[user_word]:


                        user_wordlist = [f"{key} : {value}" for key,value in item_dict[user_word].items()]+[f"[詳細資訊連結](https://ff14.huijiwiki.com/wiki/%E7%89%A9%E5%93%81:{item_dict[user_word]['簡體中文']})"]

                        embed.description = "\n".join(user_wordlist)
                        embed.set_thumbnail(url = f"https://universalis-ffxiv.github.io/universalis-assets/icon2x/{item_dict[user_word]['ID']}.png")
                        await message.reply(embed=embed, mention_author=True)
                        # await message.channel.send(embed=embed)

                    else:
                        user_wordlist = [f"{key} : {value}" for key,value in item_dict[user_word].items()]+[f"[詳細資訊連結](https://ff14.huijiwiki.com/wiki/%E7%89%A9%E5%93%81:{user_word})"]
                        embed.description = "\n".join(user_wordlist)
                        embed.set_thumbnail(url = f"https://universalis-ffxiv.github.io/universalis-assets/icon2x/{item_dict[user_word]['ID']}.png")
                        await message.reply(embed=embed, mention_author=True)

                        # await message.channel.send(embed=embed)

                else:
                    wordsim_list = difflib.get_close_matches(user_word,wordlist,10,cutoff=0.1)

                    if len(wordsim_list) > 0:
                        embed.description ="你可能要查詢的詞:\n"+"\n".join(wordsim_list)
                        await message.channel.send(embed=embed)
                    else:
                        await message.channel.send("無相關資訊")

            #查市價
            elif message.content.startswith('?bs '):
                user_word = message.content.replace('?bs ',"")
                user_word = user_word.lstrip().rstrip()
                if user_word in item_dict:
                    if "ID" in item_dict[user_word]:
                        embed.description = f"[{user_word}價格網址](https://universalis.app/market/{item_dict[user_word]['ID']})"
                        embed.set_thumbnail(url = f"https://universalis-ffxiv.github.io/universalis-assets/icon2x/{item_dict[user_word]['ID']}.png")
                        await message.reply(embed=embed, mention_author=True)

                    else:
                        embed.description = f"[{user_word}價格網址](https://universalis.app/market/{user_word})"
                        embed.set_thumbnail(url = f"https://universalis-ffxiv.github.io/universalis-assets/icon2x/{item_dict[user_word]['ID']}.png")
                        await message.reply(embed=embed, mention_author=True)

                else:
                    wordsim_list = difflib.get_close_matches(user_word,wordlist,10,cutoff=0.1)
                    if len(wordsim_list) > 0:
                        embed.description ="你可能要查詢的詞:\n"+"\n".join(wordsim_list)
                        embed.set_thumbnail(url = f"https://universalis-ffxiv.github.io/universalis-assets/icon2x/{item_dict[user_word]['ID']}.png")
                        await message.reply(embed=embed, mention_author=True)

                    else:
                        await message.reply("無相關資訊")

            elif message.content.startswith('請問'):
                user_word = message.content.replace('請問',"")
                actree = build_actree(wordlist=basequestion_list)
                send_list = [i[1][1] for i in actree.iter(user_word)]
                if len(send_list) == 1:
                    wiki_url = f"https://ff14.huijiwiki.com/wiki/{base_knowledge[send_list[0]]}"
                    await message.reply(f"這篇可以參考看看~\n{wiki_url}", mention_author=True)

呈現畫面:


上一篇
[DAY 08]新成員進來時靠bot進行說明事項
下一篇
[DAY 10]讓BOT 24小時在線(1/3)
系列文
建立FF14資訊Discord chatbot30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
C#.NET
iT邦新手 4 級 ‧ 2021-09-24 07:25:57

酷!

我要留言

立即登入留言